package cn.itsource.org.service.impl;

import cn.itsource.basic.util.PageList;
import cn.itsource.basic.util.TreeUtil;
import cn.itsource.org.domain.Department;
import cn.itsource.org.mapper.DepartmentMapper;
import cn.itsource.org.query.DepartmentQuery;
import cn.itsource.org.service.IDepartmentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DepartmentServiceImpl implements IDepartmentService {
    //注入mapper层对象
    @Autowired
    private DepartmentMapper mapper;

    //查询所有
    @Override
    public List<Department> findAll() {
        return mapper.selectAll();
    }

    //ID查询
    @Override
    public Department findOne(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    //新增或者修改,根据主键操作
    @Override
    public void saveOrUpdate(Department department) {
        if (department.getId() == null) {
            //任何字段都会被操作
//            mapper.insert(department);
            //只有设了值的字段会被新增进去
            mapper.insertSelective(department);
        } else {
            //根据主键修改,只有设置了值的字段才会被新增进去
            mapper.updateByPrimaryKeySelective(department);
        }
    }

    //删除id
    @Override
    public void delete(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    //批量删除
    @Override
    public void batchDelete(Long[] ids) {
        for (Long id : ids){
            mapper.deleteByPrimaryKey(id);
        }
    }

    //分页查询
    @Override
    public PageList<Department> findPage(DepartmentQuery query){
        //1.分页工具包
        PageHelper.startPage(query.getPage(), query.getRows());
        //2.调用方法
        List<Department> departments = mapper.findPage(query);
        //3.封装一个pageInfo对象
        PageInfo<Department> pageInfo = new PageInfo<>(departments);
        //4.封装自定义的分页对象
        PageList<Department> pageList = new PageList<>(pageInfo.getTotal(), pageInfo.getList());
        //返回数据
        return pageList;
    }

    @Override
    public List<Department> findParentDepts() {
        return mapper.findParentDepts();
    }

    private void makeParentDept(Department department){
        Long[] parentIds = department.getParentIds();
        if(parentIds != null && parentIds.length > 0){
            //1.设置parentId的值
            Long parentId = parentIds[parentIds.length - 1];
            department.setParentId(parentId);
            //2.循环  用得快捷提示:输入iter
            StringBuilder dirPath = new StringBuilder("");
            for(Long id : parentIds){
                dirPath.append("/").append(id);
            }
            //3.加上自己的主键id值
            dirPath.append("/").append(department.getId());
            department.setDirPath(dirPath.toString());
        } else {
            department.setParentId(null);
            department.setDirPath("/" + department.getId());
        }
    }

    @Override
    public List<Department> treeList() {
        //1.查询所有的部门对象集合
        List<Department> departments = mapper.selectAll();

        //2.使用工具类的方法
        List<Department> list = TreeUtil.getTree(departments);
        return list;
    }
}
